//
//  MyCollectionController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/7/28.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "MyCollectionController.h"
#import "MyCollectionCell.h"
#import "MyCollectionModel.h"

@interface MyCollectionController ()<UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *_progressHUD;
    //无收藏
    UIImageView *_noDrawImage;
    //图片url
    NSDictionary *_imageUrlDict;
    //收藏商品id
    NSString *_collectionId;
}

@property(nonatomic,strong)UITableView *myCollectionView;
@property(nonatomic,strong)NSMutableArray *collectionArray;
@property(nonatomic,strong)NSMutableArray *goodsIdArray;

@end

static NSString *const myCollectionIdentifier = @"myCollectionTableViewCell";

@implementation MyCollectionController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    //菊花加载
    _progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:_progressHUD];

    //收藏列表请求
    [self getMyCollectionRequestData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addTableView];
}

- (void)addTableView
{
    UITableView *myCollectionView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStyleGrouped];
    self.myCollectionView = myCollectionView;
    [self.view addSubview:myCollectionView];
    self.myCollectionView.rowHeight = HEIGHT/6;

    
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    
}


#pragma mark >>>>>>>>>>>收藏列表请求
- (void)getMyCollectionRequestData
{
    
    [_collectionArray removeAllObjects];
    
    NSDictionary *params = @{
                             @"type":[NSString stringWithFormat:@"%d",1],
                             @"username":[NSString stringWithFormat:@"%@",USERNAME]
                             };
//    NSLog(@"%@",params);
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/goods/favoriteList" completeBlock:^(id object) {
        
        
        NSLog(@"%@",object);
        if (JudgeBackDataCode) {
            
            NSDictionary *totalDict = [object objectForKey:@"data"];
            //商品数据
            NSArray *listArr = [totalDict objectForKey:@"list"];
            //图片
            _imageUrlDict = [totalDict objectForKey:@"picprex"];
            
            NSDictionary *dataDict;
            _collectionArray = [NSMutableArray array];
            _goodsIdArray = [NSMutableArray array];
            for (NSDictionary *tempDict in listArr) {
                //
                dataDict = [tempDict objectForKey:@"good_info"];
                //收藏id
                _collectionId = [tempDict objectForKey:@"id"];
                [_goodsIdArray addObject:_collectionId];
                
                MyCollectionModel *CollectionModel = [[MyCollectionModel alloc] initWithDictionary:dataDict error:nil];
                [_collectionArray addObject:CollectionModel];

            }
            
        }else{
            
            //暂无收藏(图)
            _noDrawImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noCollection"]];
            [self.view addSubview:_noDrawImage];
            
            [_noDrawImage mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(self.view.mas_centerX);
                make.centerY.equalTo(self.view.mas_centerY);
                make.width.mas_equalTo(WIDTH/2);
                make.height.mas_equalTo(HEIGHT/4);
            }];
            
            

        }
        
        [self.myCollectionView reloadData];
        [_progressHUD hide:YES];
        
    }];
}


#pragma mark >>>>>>>>>>>删除收藏
- (void)myCollectionDeleteDidClick:(UIButton *)button
{
    
    //弹框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定删除收藏?" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okay = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //菊花加载
        _progressHUD = [MBProgressHUD loadingInformationToView];
        [self.view addSubview:_progressHUD];
        
        //删除请求
        [self myCollectionDataDelete:button];
    }];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    
    [alert addAction:okay];
    [alert addAction:cancle];
    
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
}

- (void)myCollectionDataDelete:(UIButton *)button
{
    NSString *collectionId = _goodsIdArray[button.tag];
    
    NSDictionary *params = @{
                             @"id":[NSString stringWithFormat:@"%@",collectionId],
                             @"username":[NSString stringWithFormat:@"%@",USERNAME]
                             };
     NSLog(@"%@",params);
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"/api/goods/deleteFavorite" completeBlock:^(id object) {
        
//        NSLog(@"%@",object);
        if (JudgeBackDataCode) {
            //收藏列表请求
            [self getMyCollectionRequestData];
        }
       
        
    }];
}


#pragma mark>>>>>>>>>>>数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.collectionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    MyCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:myCollectionIdentifier];
    
    if (cell==nil) {
        
        cell = [[MyCollectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myCollectionIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    
    MyCollectionModel *collectionModel = self.collectionArray[indexPath.row];
    
    NSString *string = [NSString stringWithFormat:@"%@%@",_imageUrlDict[@"m"],collectionModel.picurl];
    [cell.collectionImg sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"businessNopict.jpg"]];
    
    cell.collectionName.text = collectionModel.title;
    cell.collectionPrice.text = [NSString stringWithFormat:@"¥ %@",collectionModel.huiyuanjia];
    
    [cell.collectionDelete addTarget:self action:@selector(myCollectionDeleteDidClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.collectionDelete.tag = indexPath.row;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


@end
